let config = require("./config")
var http = require('http');


let auth = Buffer.from(config.flowable_server.username + ":" + config.flowable_server.password).toString('base64');

let request = {
    post_file: function (path, form,) {
        var request = http.request({
            method: 'post',
            host: config.flowable_server.host,
            port: config.flowable_server.port,
            path: '/' + config.flowable_server.path + path,
            headers: {
                ...form.getHeaders(),
                "Authorization": "Basic " + auth
            }
        });
        form.pipe(request);
        return new Promise(function (resolve, reject) {
            request.on('response', function (res) {
                res.setEncoding('utf8');
                let data="";
                res.on('data', function (chunk) {
                    data+=chunk;
                });
                res.on('end', function () {
                    //console.log(data);
                    resolve({
                        code: res.statusCode,
                        data: JSON.parse(data)
                    })
                });
                res.on('error', function (e) {
                    reject(e);
                });
            });
            request.end();
        });
    },
    get: function (path) {
        return new Promise(function (resolve, reject) {
            var request = http.request({
                method: 'get',
                host: config.flowable_server.host,
                port: config.flowable_server.port,
                path: '/' + config.flowable_server.path + path,
                headers: {
                    "Content-Type": "application/json",
                    "Authorization": "Basic " + auth
                }
            },
            function (res) {
                res.setEncoding('utf8');
                let data="";
                res.on('data', function (chunk) {
                    data+=chunk;
                });
                res.on('end', function () {
                    //console.log(data);
                    resolve({
                        code: res.statusCode,
                        data: JSON.parse(data)
                    })
                });
                res.on('error', function (e) {
                    reject(e);
                });
            });
            request.end();
        });
    },
    post: function (path,data) {
        return new Promise(function (resolve, reject) {
            let content=JSON.stringify(data);
            //console.log(content);
            var request = http.request({
                method: 'post',
                host: config.flowable_server.host,
                port: config.flowable_server.port,
                path: '/' + config.flowable_server.path + path,
                headers: {
                    "Content-Type": "application/json",
                    "Accept": "application/json",
                    "Authorization": "Basic " + auth
                }
            },
            function (res) {
                res.setEncoding('utf8');
                let data="";
                res.on('data', function (chunk) {
                    data+=chunk;
                });
                res.on('end', function () {
                    //console.log(data);
                    resolve({
                        code: res.statusCode,
                        data: JSON.parse(data)
                    })
                });
                res.on('error', function (e) {
                    reject(e);
                });
            });
            request.write(content);
            request.end();
        });
    }
}

module.exports = request;